﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Controls="clr-namespace:MahApps.Metro.Controls"
                    xmlns:Converters="clr-namespace:MahApps.Metro.Converters">

    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    <Converters:ThicknessToDoubleConverter x:Key="ThicknessToDoubleConverter" />

    <!--PasswordBox Style-->
    <Style TargetType="{x:Type PasswordBox}"
           x:Key="MetroPasswordBox">
        <Setter Property="ContextMenu"
                Value="{DynamicResource TextBoxMetroContextMenu}" />
        <Setter Property="Controls:TextboxHelper.IsMonitoring"
                Value="True" />
        <Setter Property="Controls:ControlsHelper.ButtonWidth"
                Value="22" />
        <Setter Property="SelectionBrush"
                Value="{DynamicResource HighlightBrush}" />
        <Setter Property="FontFamily"
                Value="{DynamicResource ContentFontFamily}" />
        <Setter Property="FontSize"
                Value="{DynamicResource ContentFontSize}" />
        <Setter Property="BorderThickness"
                Value="1" />
        <Setter Property="MinHeight"
                Value="26" />
        <Setter Property="Background"
                Value="{DynamicResource ControlBackgroundBrush}" />
        <Setter Property="Foreground"
                Value="{DynamicResource TextBrush}" />
        <Setter Property="Padding"
                Value="0" />
        <Setter Property="BorderBrush"
                Value="{DynamicResource TextBoxBorderBrush}" />
        <Setter Property="Controls:TextboxHelper.FocusBorderBrush"
                Value="{DynamicResource TextBoxFocusBorderBrush}" />
        <Setter Property="Controls:TextboxHelper.MouseOverBorderBrush"
                Value="{DynamicResource TextBoxMouseOverBorderBrush}" />
        <!-- change SnapsToDevicePixels to true to view a better border and validation error -->
        <Setter Property="SnapsToDevicePixels"
                Value="True" />
        <Setter Property="Validation.ErrorTemplate"
                Value="{DynamicResource ValidationErrorTemplate}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type PasswordBox}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="enterGotFocus">
                            <DoubleAnimation Duration="0:0:0.2"
                                             To=".2"
                                             Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="Message" />
                        </Storyboard>
                        <Storyboard x:Key="exitGotFocus">
                            <DoubleAnimation Duration="0:0:0.2"
                                             Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="Message" />
                        </Storyboard>

                        <Storyboard x:Key="enterHasText">
                            <DoubleAnimation Duration="0:0:0.2"
                                             From=".2"
                                             To="0"
                                             Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="Message" />
                        </Storyboard>
                        <Storyboard x:Key="exitHasText">
                            <DoubleAnimation Duration="0:0:0.2"
                                             Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="Message" />
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <Grid>
                        <Rectangle x:Name="Base"
                                   Stroke="{TemplateBinding BorderBrush}"
                                   StrokeThickness="{TemplateBinding BorderThickness, Converter={StaticResource ThicknessToDoubleConverter}}"
                                   Opacity="1"
                                   Fill="{TemplateBinding Background}" />
                        <Rectangle x:Name="FocusRectangle"
                                   Visibility="Collapsed"
                                   StrokeThickness="{TemplateBinding BorderThickness, Converter={StaticResource ThicknessToDoubleConverter}}" />
                        <Grid Margin="2">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=ActualHeight, Mode=OneWay}" />
                            </Grid.ColumnDefinitions>

                            <Border x:Name="ReadOnlyVisualElement"
                                    Grid.ColumnSpan="2"
                                    Background="{x:Null}"
                                    Opacity="0" />
                            <ScrollViewer x:Name="PART_ContentHost"
                                          Grid.Column="0"
                                          BorderThickness="0"
                                          IsTabStop="False"
                                          Margin="2"
                                          VerticalAlignment="Stretch"
                                          Background="{x:Null}" />
                            <TextBlock x:Name="Message"
                                       Grid.Column="0"
                                       Text="{TemplateBinding Controls:TextboxHelper.Watermark}"
                                       Padding="{TemplateBinding Padding}"
                                       Visibility="Collapsed"
                                       Foreground="{TemplateBinding Foreground}"
                                       IsHitTestVisible="False"
                                       Opacity="0.6"
                                       HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                       VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                       Margin="6,2,6,2" />
                            <Button x:Name="PART_ClearText"
                                    Grid.Column="1"
                                    Style="{DynamicResource ChromelessButtonStyle}"
                                    FontSize="16"
                                    Foreground="{TemplateBinding Foreground}"
                                    FontFamily="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:TextboxHelper.ButtonFontFamily), Mode=OneWay}"
                                    Content="r"
                                    Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:TextboxHelper.ClearTextButton), Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}"
                                    IsTabStop="False" />
                        </Grid>
                        <Rectangle x:Name="DisabledVisualElement"
                                   Stroke="{DynamicResource ControlsDisabledBrush}"
                                   StrokeThickness="{TemplateBinding BorderThickness, Converter={StaticResource ThicknessToDoubleConverter}}"
                                   Fill="{DynamicResource ControlsDisabledBrush}"
                                   IsHitTestVisible="False"
                                   Opacity="0" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Password, Mode=OneWay}"
                                     Value="">
                            <Setter TargetName="Message"
                                    Property="Visibility"
                                    Value="Visible" />
                        </DataTrigger>

                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(Controls:TextboxHelper.ClearTextButton)}"
                                     Value="False">
                            <Setter TargetName="PART_ContentHost"
                                    Property="Grid.ColumnSpan"
                                    Value="2" />
                            <Setter TargetName="Message"
                                    Property="Grid.ColumnSpan"
                                    Value="2" />
                        </DataTrigger>
                        <Trigger Property="IsMouseOver"
                                 Value="True">
                            <Setter TargetName="FocusRectangle"
                                    Property="Visibility"
                                    Value="Visible" />
                            <Setter TargetName="FocusRectangle"
                                    Property="Stroke"
                                    Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:TextboxHelper.MouseOverBorderBrush)}" />
                        </Trigger>
                        <Trigger Property="IsEnabled"
                                 Value="False">
                            <Setter TargetName="DisabledVisualElement"
                                    Property="Opacity"
                                    Value="0.6" />
                        </Trigger>
                        <Trigger Property="IsFocused"
                                 Value="True">
                            <Setter TargetName="FocusRectangle"
                                    Property="Visibility"
                                    Value="Visible" />
                            <Setter TargetName="FocusRectangle"
                                    Property="Stroke"
                                    Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:TextboxHelper.FocusBorderBrush)}" />
                        </Trigger>
                        <Trigger SourceName="PART_ClearText"
                                 Property="IsMouseOver"
                                 Value="True">
                            <Setter TargetName="PART_ClearText"
                                    Property="Background"
                                    Value="{DynamicResource GrayBrush8}" />
                            <Setter TargetName="PART_ClearText"
                                    Property="Foreground"
                                    Value="{DynamicResource AccentColorBrush}" />
                        </Trigger>
                        <Trigger SourceName="PART_ClearText"
                                 Property="IsPressed"
                                 Value="True">
                            <Setter TargetName="PART_ClearText"
                                    Property="Background"
                                    Value="{DynamicResource BlackBrush}" />
                            <Setter TargetName="PART_ClearText"
                                    Property="Foreground"
                                    Value="{DynamicResource WhiteBrush}" />
                        </Trigger>

                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="Controls:TextboxHelper.HasText"
                                           Value="False" />
                                <Condition Property="IsFocused"
                                           Value="True" />
                            </MultiTrigger.Conditions>
                            <MultiTrigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource enterGotFocus}" />
                            </MultiTrigger.EnterActions>
                            <MultiTrigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource exitGotFocus}" />
                            </MultiTrigger.ExitActions>
                        </MultiTrigger>

                        <Trigger Property="Controls:TextboxHelper.HasText"
                                 Value="True">
                            <Setter TargetName="Message"
                                    Property="Visibility"
                                    Value="Visible" />
                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource enterHasText}" />
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource exitHasText}" />
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--Button Command PasswordBox Style-->
    <Style TargetType="{x:Type PasswordBox}"
           BasedOn="{StaticResource MetroPasswordBox}"
           x:Key="MetroButtonPasswordBox">
        <Setter Property="Controls:TextboxHelper.ButtonTemplate"
                Value="{DynamicResource ChromelessButtonTemplate}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type PasswordBox}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="enterGotFocus">
                            <DoubleAnimation Duration="0:0:0.2"
                                             To=".2"
                                             Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="Message" />
                        </Storyboard>
                        <Storyboard x:Key="exitGotFocus">
                            <DoubleAnimation Duration="0:0:0.2"
                                             Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="Message" />
                        </Storyboard>

                        <Storyboard x:Key="enterHasText">
                            <DoubleAnimation Duration="0:0:0.2"
                                             From=".2"
                                             To="0"
                                             Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="Message" />
                        </Storyboard>
                        <Storyboard x:Key="exitHasText">
                            <DoubleAnimation Duration="0:0:0.2"
                                             Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="Message" />
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <Grid>
                        <Rectangle x:Name="Base"
                                   Stroke="{TemplateBinding BorderBrush}"
                                   StrokeThickness="{TemplateBinding BorderThickness, Converter={StaticResource ThicknessToDoubleConverter}}"
                                   Opacity="1"
                                   Fill="{TemplateBinding Background}" />
                        <Rectangle x:Name="FocusRectangle"
                                   Visibility="Collapsed"
                                   StrokeThickness="{TemplateBinding BorderThickness, Converter={StaticResource ThicknessToDoubleConverter}}" />
                        <Grid x:Name="PART_InnerGrid"
                              Margin="2">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition x:Name="ButtonColumn"
                                                  Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ControlsHelper.ButtonWidth), Mode=OneWay}" />
                            </Grid.ColumnDefinitions>

                            <Border x:Name="ReadOnlyVisualElement"
                                    Grid.ColumnSpan="2"
                                    Background="{x:Null}"
                                    Opacity="0" />
                            <ScrollViewer x:Name="PART_ContentHost"
                                          Grid.Column="0"
                                          BorderThickness="0"
                                          IsTabStop="False"
                                          Margin="2"
                                          VerticalAlignment="Stretch"
                                          Background="{x:Null}" />
                            <TextBlock x:Name="Message"
                                       Grid.Column="0"
                                       Text="{TemplateBinding Controls:TextboxHelper.Watermark}"
                                       Padding="{TemplateBinding Padding}"
                                       Visibility="Collapsed"
                                       Foreground="{TemplateBinding Foreground}"
                                       IsHitTestVisible="False"
                                       Opacity="0.6"
                                       HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                       VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                       Margin="6,2,6,2" />
                            <Button x:Name="PART_ClearText"
                                    Grid.Column="1"
                                    Style="{DynamicResource ChromelessButtonStyle}"
                                    FontSize="16"
                                    Foreground="{TemplateBinding Foreground}"
                                    FontFamily="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:TextboxHelper.ButtonFontFamily), Mode=OneWay}"
                                    Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:TextboxHelper.ButtonContent), Mode=OneWay}"
                                    Template="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:TextboxHelper.ButtonTemplate), Mode=OneWay}"
                                    IsTabStop="False" />
                        </Grid>
                        <Rectangle x:Name="DisabledVisualElement"
                                   Stroke="{DynamicResource ControlsDisabledBrush}"
                                   StrokeThickness="{TemplateBinding BorderThickness, Converter={StaticResource ThicknessToDoubleConverter}}"
                                   Fill="{DynamicResource ControlsDisabledBrush}"
                                   IsHitTestVisible="False"
                                   Opacity="0" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Password, Mode=OneWay}"
                                     Value="">
                            <Setter TargetName="Message"
                                    Property="Visibility"
                                    Value="Visible" />
                        </DataTrigger>

                        <Trigger Property="IsMouseOver"
                                 Value="True">
                            <Setter TargetName="FocusRectangle"
                                    Property="Visibility"
                                    Value="Visible" />
                            <Setter TargetName="FocusRectangle"
                                    Property="Stroke"
                                    Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:TextboxHelper.MouseOverBorderBrush)}" />
                        </Trigger>
                        <Trigger Property="IsEnabled"
                                 Value="False">
                            <Setter TargetName="DisabledVisualElement"
                                    Property="Opacity"
                                    Value="0.6" />
                        </Trigger>
                        <Trigger Property="IsFocused"
                                 Value="True">
                            <Setter TargetName="FocusRectangle"
                                    Property="Visibility"
                                    Value="Visible" />
                            <Setter TargetName="FocusRectangle"
                                    Property="Stroke"
                                    Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:TextboxHelper.FocusBorderBrush)}" />
                        </Trigger>
                        <Trigger SourceName="PART_ClearText"
                                 Property="IsMouseOver"
                                 Value="True">
                            <Setter TargetName="PART_ClearText"
                                    Property="Background"
                                    Value="{DynamicResource GrayBrush8}" />
                            <Setter TargetName="PART_ClearText"
                                    Property="Foreground"
                                    Value="{DynamicResource AccentColorBrush}" />
                        </Trigger>
                        <Trigger SourceName="PART_ClearText"
                                 Property="IsPressed"
                                 Value="True">
                            <Setter TargetName="PART_ClearText"
                                    Property="Background"
                                    Value="{DynamicResource BlackBrush}" />
                            <Setter TargetName="PART_ClearText"
                                    Property="Foreground"
                                    Value="{DynamicResource WhiteBrush}" />
                        </Trigger>

                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="Controls:TextboxHelper.HasText"
                                           Value="False" />
                                <Condition Property="IsFocused"
                                           Value="True" />
                            </MultiTrigger.Conditions>
                            <MultiTrigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource enterGotFocus}" />
                            </MultiTrigger.EnterActions>
                            <MultiTrigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource exitGotFocus}" />
                            </MultiTrigger.ExitActions>
                        </MultiTrigger>

                        <Trigger Property="Controls:TextboxHelper.HasText"
                                 Value="True">
                            <Setter TargetName="Message"
                                    Property="Visibility"
                                    Value="Visible" />
                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource enterHasText}" />
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource exitHasText}" />
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>
